/**
 * Copyright:Copyright(c)2014-2017
 * Company:厦门市易致达物联网科技有限公司
 * @version 1.0
 */
package shop.controller.manage.activity;
import shop.core.common.bean.JSONResult;
import shop.core.common.bean.ManageContainer;
import shop.core.common.dao.page.PagerModel;
import shop.core.util.KeyValueHelper;
import shop.core.Services;
import shop.core.util.MD5;
import shop.services.manage.activity.ActivityService;
import shop.services.manage.activity.bean.Activity;
import shop.core.BaseController;
import shop.core.RequestHolder;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import shop.services.manage.activityjoin.ActivityjoinService;
import shop.services.manage.activityjoin.bean.Activityjoin;
import shop.services.manage.activitypicture.ActivitypictureService;
import shop.services.manage.activitypicture.bean.Activitypicture;
import shop.services.manage.system.bean.User;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**   
 * @类名称：ActivityAction      
 * @创建人：wzl
 * @创建时间：2018-01-03 下午16:07:18      
 * @版本号：1.0
 * @描述：     
 */
@Controller
@RequestMapping("/manage/activity/")
public class ActivityAction extends BaseController<Activity> {
    private final  static  String UPLOAD_ADDRESS_FRONT="front";
    private final  static  String UPLOAD_ADDRESS_MANAGE="manage";
    private static final Logger logger = LoggerFactory.getLogger(ActivityAction.class);
    @Autowired
    private ActivityService activityService;
    @Autowired
    private ActivityjoinService activityjoinService;
    @Autowired
    private ActivitypictureService activitypictureService;
    private static final String page_toList = "/manage/activity/activityList";
    private static final String page_toAdd = "/manage/activity/activityAdd";
    private static final String page_toEdit = "/manage/activity/activityEdit";
    private static final String page_toActivityJoin = "/manage/activityjoin/activityJoin";
    private static final String page_toActivityPicture = "/manage/activitypicture/activityPicture";
    private static final String page_toActivityPictureCheck = "/manage/activitypicture/activityPictureCheck";
    public ActivityService getService() {
        return activityService;
    }

    private ActivityAction(){
        super.page_toList = page_toList;
        super.page_toAdd = page_toAdd;
        super.page_toEdit = page_toEdit;
    }


    /**
     * 跳转到活动参与列表页面
     *
     * @return
     */
    @RequestMapping("toActivityJoin")
    public String toActivityJoin() {
        return page_toActivityJoin;
    }

    /**
     * 跳转到活动照片列表
     *
     * @return
     */
    @RequestMapping("toActivityPicture ")
    public String toActivityPicture () {
        return page_toActivityPicture;
    }

    /**
     * 跳转到活动照片审核页面
     *
     * @return
     */
    @RequestMapping("toActivityPictureCheck ")
    public String toActivityPictureCheck (@ModelAttribute("e") Activitypicture activitypicture, ModelMap model) {
        activitypicture = activitypictureService.selectOne(activitypicture);
        model.addAttribute("e", activitypicture);
        return page_toActivityPictureCheck;
    }

    /**
     * 查询活动列表
     *
     * @param request 请求
     * @param activity       对象参数
     * @return 分页数据模型
     */
    @RequestMapping("selectActivityPage")
    @ResponseBody
    public PagerModel selectActivityPage(HttpServletRequest request, Activity activity) {
        int offset = 0;
        int pageSize = 10;
        int start=Integer.parseInt(request.getParameter("start"));
        int length= Integer.parseInt(request.getParameter("length"));
        if(start>0)
            offset=start;
        if(length>0)
            pageSize=length;
        activity.setOffset(offset);
        activity.setPageSize(pageSize);
        PagerModel pager = activityService.selectActivityPage(activity);
        pager.setRecordsTotal(pager.getTotal());
        pager.setRecordsFiltered(pager.getTotal());
        return pager;
    }

    /**
     * 查询活动报名记录
     *
     * @param request 请求
     * @param activityjoin       对象参数
     * @return 分页数据模型
     */
    @RequestMapping("selectActivityJoinPage")
    @ResponseBody
    public PagerModel selectActivityJoinPage(HttpServletRequest request, Activityjoin activityjoin) {
        int offset = 0;
        int pageSize = 10;
        int start=Integer.parseInt(request.getParameter("start"));
        int length= Integer.parseInt(request.getParameter("length"));
        if(start>0)
            offset=start;
        if(length>0)
            pageSize=length;
        activityjoin.setOffset(offset);
        activityjoin.setPageSize(pageSize);
        PagerModel pager = activityjoinService.selectActivityJoinPage(activityjoin);
        pager.setRecordsTotal(pager.getTotal());
        pager.setRecordsFiltered(pager.getTotal());
        return pager;
    }

    /**
     * 查询活动照片
     *
     * @param request 请求
     * @param e       对象参数
     * @return 分页数据模型
     */
    @RequestMapping("selectActivityPicturePage")
    @ResponseBody
    public PagerModel selectActivityPicturePage(HttpServletRequest request, Activitypicture e) {
        int offset = 0;
        int pageSize = 10;
        int start=Integer.parseInt(request.getParameter("start"));
        int length= Integer.parseInt(request.getParameter("length"));
        if(start>0)
            offset=start;
        if(length>0)
            pageSize=length;
        e.setOffset(offset);
        e.setPageSize(pageSize);
        PagerModel pager = activitypictureService.selectActivityPicturePage(e);
        pager.setRecordsTotal(pager.getTotal());
        pager.setRecordsFiltered(pager.getTotal());
        return pager;
    }

    /**
     * 上传活动照片
     *
     * @return jsonResult
     * @throws Exception
     */
    @RequestMapping(value = "insertActivityPicture", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult insertActivityPicture(HttpServletRequest request,Activitypicture activitypicture) throws Exception {
        JSONResult jsonResult = new JSONResult();
        User user = (User) request.getSession().getAttribute(ManageContainer.manage_session_user_info);
        activitypicture.setUserID(user.getId());
        activitypicture.setUploadAddress(UPLOAD_ADDRESS_MANAGE);
        activitypictureService.insert(activitypicture);
        return jsonResult;
    }
}
